﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace ExtensionHelper.Xml
{
    public static partial class XmlHelper
    {
        /// <summary>
        /// XElement的定义中查询包含name的XObject枚举
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static IEnumerable<XObject> FindName(this XElement sourse, string name)
        {
            if (sourse.Attributes().Any())
            {
                foreach (XAttribute att in sourse.Attributes())
                {
                    string attname = att.Name.ToString();
                    if (attname.Contains(name))
                        yield return att;
                }
            }
            if (sourse.Elements().Any())
            {
                foreach (XElement child in sourse.Elements())
                    foreach (XObject o in child.FindName(name))
                        yield return o;
            }
            else
            {
                string contents = sourse.Name.ToString();
                if (contents.Contains(name))
                    yield return sourse;
            }
        }

        /// <summary>
        /// XElement的值中查询包含value的XObject枚举
        /// </summary>
        /// <param name="source"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IEnumerable<XObject> FindValue(this XElement source, string value)
        {
            if (source.Attributes().Any())
            {
                foreach (XAttribute att in source.Attributes())
                {
                    string Contents = (string)att;
                    if (Contents.Contains(value))
                        yield return att;
                }
            }
            if (source.Elements().Any())
            {
                foreach (XElement child in source.Elements())
                {
                    foreach (XObject o in child.FindValue(value))
                        yield return o;
                }
            }
            else
            {
                string Contents = (string)source;
                if (Contents.Contains(value))
                    yield return source;

            }
        }

        /// <summary>
        /// 转xml
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string Xml(this XElement element)
        {
            StringBuilder buffer = new StringBuilder(1000);
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(buffer));
            element.WriteTo(writer);
            return buffer.ToString();
        }

        /// <summary>
        /// 转xml
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>       
        public static string ToXml(this XmlDocument doc)
        {
            StringBuilder buffer = new StringBuilder(1000);
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(buffer));
            doc.WriteContentTo(writer);
            return buffer.ToString();
        }
    }

}
